/**
 * Created by chengming on 18/5/27.
 * version 1.0
 */
const BaseJsonAdapter = require('../base/BaseJsonAdapter');
const iconv = require("iconv-lite");
const vm = require('vm');
const request = require('request-promise');
const cheerio = require('cheerio');
const logger = require('../../conf/logger');
const querystring = require("querystring")
const urlencode = require("urlencode")
const urlTool = require('url');

/**
 */
module.exports = class BlockCrypto extends BaseJsonAdapter{

    async reqJson(){
        const body = await this.doRequest();
      let decodeBody = iconv.decode(body, this.encoding).toString();
      let obj = JSON.parse(decodeBody);
      let html = obj.html;

      let data = this.parseHtml(html,"div.column--main")
      return data;
	}
  parseHtml(html, linkRule){
    //将解析后的内容加载进解析器
    let $ = cheerio.load(html);

    let contents = [];
    let that = this;
    let rangeText = '';
    $(linkRule).each( function() {

      let title = $(this).find('h3').text().trim();

      let $link=$(this).find('.entry-header a');
      let href = '';
      if($link.attr('href')){
        href = urlTool.resolve(that.requestUrl, $link.attr('href'));
      }

      if(title.length != 0 || href.length != 0){
        contents.push({
          title:title,
          href:href
        });
        rangeText +=title+href;
      }
    });
    return {
      rangeText: rangeText,
      contents: contents
    };
  }
}
